package com.bawei.demo_shoppingcar.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.bean.ShoppingDataBean;
import com.bawei.demo_shoppingcar.widget.BaseRecyclerAdapter;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;


public class ShoppingAdapter extends BaseRecyclerAdapter<ShoppingDataBean.DataBean.DefaultGoodsListBean, RecyclerView.ViewHolder> {
    Context context;
    ShoppingDataBean.DataBean data;
    private MyImageLayoutItemClickListener mItemClickListener;
    private MyDefaultItemClickListener mdefaultItemClickListener;

    /**
     * 创建ad5的回调接口
     */
    public interface MyImageLayoutItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 创建default商品的回调接口
     */
    public interface MyDefaultItemClickListener {
        void onItemClick(View view, int position);
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setDefaultItemClickListener(MyDefaultItemClickListener myItemClickListener) {
        this.mdefaultItemClickListener = myItemClickListener;
    }

    /**
     * 在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
     *
     * @param myItemClickListener
     */
    public void setImageLayoutItemClickListener(MyImageLayoutItemClickListener myItemClickListener) {
        this.mItemClickListener = myItemClickListener;
    }


    public ShoppingAdapter(Context context, ShoppingDataBean.DataBean data) {
        super(context, (ArrayList<ShoppingDataBean.DataBean.DefaultGoodsListBean>) data.getDefaultGoodsList());
        this.context = context;
        this.data = data;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = null;
        RecyclerView.ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_news_recyclerview, viewGroup, false);
                viewHolder = new DefaultShoppingViewHolder(view, mdefaultItemClickListener);
                break;
            case 1:
                view = LayoutInflater.from(mContext).inflate(R.layout.tab_item_view, viewGroup, false);
                viewHolder = new Image_Layout(view, mItemClickListener);
                break;

        }
        return viewHolder;
    }

    @Override
    public int getItemViewType(int position) {
        List<ShoppingDataBean.DataBean.Ad5Bean> ad5 = data.getAd5();

        if (position < ad5.size()) {

            return 1;
        }

        return 0;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        List<ShoppingDataBean.DataBean.Ad5Bean> ad5 = data.getAd5();
        //根据条目的类型给holder中的控件填充数据
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case 0:
                DefaultShoppingViewHolder hodler = (DefaultShoppingViewHolder) viewHolder;
                List<ShoppingDataBean.DataBean.DefaultGoodsListBean> defaultGoodsList = data.getDefaultGoodsList();
                ShoppingDataBean.DataBean.DefaultGoodsListBean bean = defaultGoodsList.get(position - ad5.size());
                hodler.name.setText(bean.getGoods_name());
                hodler.market_price.setText("¥ " + bean.getMarket_price());
                hodler.market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                hodler.shop_price.setText("¥ " + bean.getShop_price());
                ImageLoader.getInstance().displayImage(bean.getGoods_img(), hodler.image, ContentData.options);
                break;

            case 1:
                Image_Layout image_hodler = (Image_Layout) viewHolder;
                ShoppingDataBean.DataBean.Ad5Bean ad5Bean = ad5.get(position);
                image_hodler.text.setText(ad5Bean.getTitle());
                ImageLoader.getInstance().displayImage(ad5Bean.getImage(), image_hodler.image, ContentData.options);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                image_hodler.image.setLayoutParams(params);
                image_hodler.image.setScaleType(ImageView.ScaleType.FIT_XY);

                break;

        }

    }

    @Override
    public int getItemCount() {
        List<ShoppingDataBean.DataBean.Ad5Bean> ad5 = data.getAd5();
        return data.getDefaultGoodsList().size() + ad5.size();
    }

    public class DefaultShoppingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        MyDefaultItemClickListener mdefaultItemClickListener;
        private final ImageView image;
        private final TextView name;
        private final TextView shop_price;
        private final TextView market_price;

        public DefaultShoppingViewHolder(View view, MyDefaultItemClickListener mdefaultItemClickListener) {
            super(view);
            image = (ImageView) view.findViewById(R.id.goods_image);
            name = (TextView) view.findViewById(R.id.goods_name);
            shop_price = (TextView) view.findViewById(R.id.goods_shop_price);
            market_price = (TextView) view.findViewById(R.id.goods_market_price);


            this.mdefaultItemClickListener = mdefaultItemClickListener;
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mdefaultItemClickListener != null) {
                mdefaultItemClickListener.onItemClick(v, getPosition());
            }

        }
    }


    public class Image_Layout extends RecyclerView.ViewHolder implements View.OnClickListener {

        private MyImageLayoutItemClickListener mListener;
        private final TextView text;
        private final ImageView image;

        public Image_Layout(View itemView, MyImageLayoutItemClickListener myItemClickListener) {
            super(itemView);
            text = (TextView) itemView.findViewById(R.id.tab_text);
            image = (ImageView) itemView.findViewById(R.id.tab_image);

            //将全局的监听赋值给接口
            this.mListener = myItemClickListener;

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

        }
    }


}